package com.ssm.controller;

import com.ssm.exception.ResultCode;
import com.ssm.exception.SysException;
import com.ssm.pojo.Score;
import com.ssm.service.ScoreService;
import com.ssm.service.impl.ScoreServiceImpl;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class ScoreController {

    @Autowired
    private ScoreService scoreService;

    @ResponseBody
    @GetMapping("/scores")
    public List<Score> selectAll(){
        return scoreService.selectAll();
    }

    @ResponseBody
    @GetMapping("/score/{id}")
    public Score selectById(@PathVariable("id") int id){
       return scoreService.selectById(id);
    }

    @ResponseBody
    @PutMapping("/score/{score}/{id}")
    public String updateScore(@PathVariable int score,@PathVariable int id) throws SysException {
        try {
            int i = scoreService.updateById(score,id);
        } catch (Exception e) {
            throw new SysException(ResultCode.UNKNOWM_ERROR);
        }
        return "put:success";
    }

    @ResponseBody
    @PostMapping("/score")
    public String insert(Score score) throws SysException {
        try {
            int i = scoreService.insertOneData(score);
        } catch (Exception e) {
            throw new SysException(ResultCode.UNKNOWM_ERROR);
        }
        return "post:success";
    }
    @ResponseBody
    @DeleteMapping("/score/{id}")
    public String delete(@PathVariable int id) throws SysException {
        try {
            scoreService.deleteById(id);
        } catch (Exception e) {
            throw new SysException(ResultCode.UNKNOWM_ERROR);
        }
        return "delete:success";
    }
    @RequestMapping("/hello")
    public String toHello(){
        return "hello";
    }


    @RequestMapping("/testException")
    public String testException() throws SysException {
        try {
            int i = 10/0;
        } catch (Exception e) {
            throw new SysException(ResultCode.UNKNOWM_ERROR);
        }
        return "hello";
    }
}
